#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#include <gen/QL/Math/Optimization/OptimizationMethod.h>
#pragma unmanaged 
#include <ql\math\optimization\levenbergmarquardt.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

#undef HANDLE
#undef ABSTRACT
#undef STRUCT
namespace Cephei { namespace QL { namespace Math { namespace Optimization {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of ILevenbergMarquardt
	public ref class CLevenbergMarquardt : 
            public COptimizationMethod,
            public Cephei::QL::Math::Optimization::ILevenbergMarquardt
	{
	protected: 
		boost::shared_ptr<QuantLib::LevenbergMarquardt>* _ppLevenbergMarquardt;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::LevenbergMarquardt>* _phLevenbergMarquardt;
#endif
		Object^ _LevenbergMarquardtOwner;     // reference to object that manages the storage for this object
	internal:
		CLevenbergMarquardt (Microsoft::FSharp::Core::FSharpOption<Double>^ epsfcn, Microsoft::FSharp::Core::FSharpOption<Double>^ xtol, Microsoft::FSharp::Core::FSharpOption<Double>^ gtol);
        CLevenbergMarquardt (boost::shared_ptr<QuantLib::LevenbergMarquardt>& childNative, Object^ owner);
        CLevenbergMarquardt (QuantLib::LevenbergMarquardt& childNative, Object^ owner);
        CLevenbergMarquardt (CLevenbergMarquardt^ copy);
        CLevenbergMarquardt (System::Type^ t);
#ifdef STRUCT
        CLevenbergMarquardt (QuantLib::LevenbergMarquardt childNative);
#endif       
#ifdef HANDLE
		CLevenbergMarquardt (QuantLib::Handle<QuantLib::LevenbergMarquardt>& childNative, Object^ owner);
		CLevenbergMarquardt (QuantLib::Handle<QuantLib::LevenbergMarquardt> childNative);
#endif
		virtual ~CLevenbergMarquardt ();
		!CLevenbergMarquardt ();

	internal:
		QuantLib::LevenbergMarquardt& GetReference ();
		boost::shared_ptr<QuantLib::LevenbergMarquardt>& GetShared ();
		QuantLib::LevenbergMarquardt* GetPointer ();
        void SetLevenbergMarquardt (boost::shared_ptr<QuantLib::LevenbergMarquardt> native)
        {
            if (_ppLevenbergMarquardt != NULL)
                delete _ppLevenbergMarquardt;
            _ppLevenbergMarquardt = new boost::shared_ptr<QuantLib::LevenbergMarquardt> (native);
            SetOptimizationMethod (boost::dynamic_pointer_cast<QuantLib::OptimizationMethod> (*_ppLevenbergMarquardt));
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::LevenbergMarquardt>& GetHandle ();
#endif
		virtual bool HasNative () override;
    public:
        property Int32 GetInfo 
        {
		    virtual Int32 get () ;
        }
		virtual QL::Math::Optimization::EndCriteria::TypeEnum Minimize (Cephei::QL::Math::Optimization::IProblem^ P, Cephei::QL::Math::Optimization::IEndCriteria^ endCriteria) ;
		virtual Cephei::QL::Math::Optimization::ILevenbergMarquardt^ Fcn (Int32 m, Int32 n, Double x, Double fvec, Int32 iflag) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CLevenbergMarquardt_Factory : public System::MarshalByRefObject,  public ILevenbergMarquardt_Factory
	{
	public:
        virtual ILevenbergMarquardt^ Create (Microsoft::FSharp::Core::FSharpOption<Double>^ epsfcn, Microsoft::FSharp::Core::FSharpOption<Double>^ xtol, Microsoft::FSharp::Core::FSharpOption<Double>^ gtol);
    };
   
/*Cephei*/ } /*QL*/ } /*Math*/ } /*Optimization */}
